﻿using System;
using System.Collections.Generic;
using SFML.Graphics;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Transformable_Engine_v2.Engine.Graphics
{
    public static class SpriteSheet
    {
        private static Dictionary<string, Texture> Textures;
        private static Dictionary<string, Dictionary<Enum, Sprite>> Sprites;

        public static void Initialize()
        {
            Textures = new Dictionary<string, Texture>();
            Sprites = new Dictionary<string, Dictionary<Enum, Sprite>>();
        }

        public static void UnloadContent()
        {
            Initialize();
        }

        public static bool AddTexture(string texturePath)
        {
            if(!Textures.ContainsKey(texturePath))
            {
                Textures.Add(texturePath, new Texture(texturePath));
                Sprites.Add(texturePath, new Dictionary<Enum, Sprite>());
                return true;
            }

            return false;
        }

        public static void AddSprite(string texturePath, Enum textureName, TE_Rectangle textureArea)
        {
            Sprites[texturePath].Add(textureName, new Sprite(textureArea, Textures[texturePath]));
        }

        public static Sprite GetSprite(string texturePath, Enum textureName)
        {
            return Sprites[texturePath][textureName];
        }

        public static Sprite GetSpriteAlphabet<T>(string texturePath, string letter)
        {
            if(Enum.IsDefined(typeof(T), letter))
            {
                return GetSprite(texturePath, (Enum) Enum.Parse(typeof(T), letter));
            }

            return null;
        }

        public enum AddEnumOrientation
        {
            Horizontal,
            Vertical
        }

        public static void AddEnumList<T>(string texturePath, Enum startValue, Enum endValue, TE_Rectangle startPosition, int frameWidth)
        {
            for (int i = 0; i <= Convert.ToInt32(endValue) - Convert.ToInt32(startValue); i++)
            {
                int value = i + Convert.ToInt32(startValue);
                Enum type = (Enum)Enum.ToObject(typeof(T), value);

                int x = i % frameWidth;
                int y = (i - x) / frameWidth;

                AddSprite(texturePath, type, new TE_Rectangle(startPosition.X + startPosition.Width*x, 
                                                              startPosition.Y + startPosition.Height*y, 
                                                              startPosition.Width,
                                                              startPosition.Height));
            }
        }

        public static void AddEnumList<T>(string texturePath, Enum startValue, Enum endValue, TE_Rectangle startPosition, AddEnumOrientation orientation = AddEnumOrientation.Horizontal)
        {
            for (int i = 0; i <= Convert.ToInt32(endValue) - Convert.ToInt32(startValue); i++)
            {
                int value = i + Convert.ToInt32(startValue);
                Enum type = (Enum)Enum.ToObject(typeof(T), value);

                switch(orientation)
                {
                    case AddEnumOrientation.Horizontal:
                        AddSprite(texturePath, type, new TE_Rectangle(startPosition.X + startPosition.Width*i, 
                                                                      startPosition.Y, 
                                                                      startPosition.Width,
                                                                      startPosition.Height));
                        break;
                    case AddEnumOrientation.Vertical:
                        AddSprite(texturePath, type, new TE_Rectangle(startPosition.X, 
                                                                      startPosition.Y + startPosition.Height*i,
                                                                      startPosition.Width,
                                                                      startPosition.Height));
                        break;
                }
            }
        }

    }
}